package com.yyg.boot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/5/18
 * @Description Description
 */
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    //@Override
    //protected void configure(HttpSecurity http) throws Exception {
        //http.httpBasic()                // 简单的HttpBasic登录方式
        //http.formLogin()              // 提供一个登录的表单
                //.and()
                //.authorizeRequests()  // 授权配置
                //.anyRequest()         // 所有请求
                //.authenticated();     // 都需要认证
    //}

    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
    }

    //内存方式创建用户
//    @Override
//    public void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication()
//                .withUser("admin").password("123456").roles("ADMIN")
//                .and()
//                .withUser("member").password("123456").roles("MEMBER");
//    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                //passwordEncoder是对密码的加密处理，如果user中密码没有加密，则可以不加此方法。注意加密请使用security自带的加密方式。
                .passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf()
                //禁用了 csrf 功能
                .disable()
                //限定签名成功的请求
                .authorizeRequests()
                .antMatchers("/employee/*")
                //对employee 下的接口,需要 MEMBER 或者 ADMIN 权限
                .hasAnyRole("MEMBER","ADMIN")
                //对employee/login接口直接放行,不限制
                .antMatchers("/employee/login")
                .permitAll()
                //对admin下的接口 需要ADMIN权限
                .antMatchers("/admin/**")
                .hasRole("ADMIN")
                //不拦截 oauth 开放的资源
                .antMatchers("/oauth/**")
                .permitAll()
                //其他没有限定的请求，允许访问
                .anyRequest()
                .permitAll()
                .and()
                //对于没有配置权限的其他请求允许匿名访问
                .anonymous()
                .and()
                //使用 spring security 默认登录页面
                .formLogin()
                .and()
                //启用http 基础验证
                .httpBasic();
    }

}
